﻿Imports MeoTom.Core.DAL

Namespace BLL
    Public Class AdminClass
        Private _UserID As Integer
        Private _UserName As String
        Private _Password As String
        Private _Email As String
        Private _Permission As Integer
        Private _Status As Integer

        Public Property UserID() As Integer
            Get
                Return _UserID
            End Get
            Set(ByVal value As Integer)
                _UserID = value
            End Set
        End Property
        Public Property UserName() As String
            Get
                Return _UserName
            End Get
            Set(ByVal value As String)
                _UserName = value
            End Set
        End Property
        Public Property Password() As String
            Get
                Return _Password
            End Get
            Set(ByVal value As String)
                _Password = value
            End Set
        End Property
        Public Property Email() As String
            Get
                Return _Email
            End Get
            Set(ByVal value As String)
                _Email = value
            End Set
        End Property
        Public Property Permission() As Integer
            Get
                Return _Permission
            End Get
            Set(ByVal value As Integer)
                _Permission = value
            End Set
        End Property
        Public Property Status() As Integer
            Get
                Return _Status
            End Get
            Set(ByVal value As Integer)
                _Status = value
            End Set
        End Property

        Public objAdminPrv As New AdminProvider

        Public Sub New()
            objAdminPrv = New AdminProvider()
        End Sub

        Public Sub Create()
            objAdminPrv.Create(Me)
        End Sub

        Public Sub Update()
            objAdminPrv.Update(Me)
        End Sub

        Public Sub Delete()
            objAdminPrv.Delete(Me)
        End Sub

        Public Function GetAll() As DataSet
            Return objAdminPrv.GetAll(Me)
        End Function

        Public Function ResetPassword() As Boolean
            Dim b As Boolean = False
            Try
                Dim reader As SqlClient.SqlDataReader = objAdminPrv.ResetPassword(Me)
                If reader.Read Then
                    If reader("ReturnValue").ToString = "1" Then
                        b = True
                    Else
                        b = False
                    End If
                End If
                reader.Close()
            Catch ex As Exception

            End Try
            Return b
        End Function

        Public Function Login() As Boolean
            Dim b As Boolean = False
            Try
                Dim reader As SqlClient.SqlDataReader = objAdminPrv.Login(Me)
                If reader.Read() Then
                    Me.UserName = reader("UserName").ToString()
                    Me.UserID = Integer.Parse(reader("UserID").ToString())
                    Me.Email = reader("Email").ToString()
                    Me.Permission = Integer.Parse(reader("Permission").ToString)
                    Me.Status = Integer.Parse(reader("Status").ToString)
                    b = True
                Else
                    b = False
                End If
                reader.Close()
            Catch ex As Exception

            End Try
            Return b
        End Function

        Public Sub GetByID()
            Try
                Dim reader As SqlClient.SqlDataReader = objAdminPrv.GetByID(Me)
                If reader.Read Then
                    Me.UserName = reader("UserName").ToString()
                    Me.UserID = Integer.Parse(reader("UserID").ToString())
                    Me.Email = reader("Email").ToString()
                    Me.Permission = Integer.Parse(reader("Permission").ToString)
                    Me.Status = Integer.Parse(reader("Status").ToString)
                End If
                reader.Close()

            Catch ex As Exception

            End Try
        End Sub
        Public Function CheckExitsAdmin() As Boolean
            Dim reader As SqlClient.SqlDataReader = objAdminPrv.CheckExitsAdmin(Me)
            If reader.Read Then
                If reader("Avaiable").ToString = "Exists" Then
                    reader.Close()
                    Return True
                Else
                    reader.Close()
                    Return False
                End If
            Else
                reader.Close()
                Return False
            End If
        End Function

        Public Function CheckEmailAvaiable() As Boolean
            Dim reader As SqlClient.SqlDataReader
            reader = objAdminPrv.CheckEmailAvaiable(Me)

            If reader.Read Then
                If reader("Avaiable").ToString = "Exists" Then
                    reader.Close()
                    Return True
                Else
                    reader.Close()
                    Return False
                End If
            Else
                reader.Close()
                Return False
            End If

        End Function

        Public Function GetNewPassword() As String
            Dim strReturn As String = ""
            Dim rnd As New Utilities.RandomStrings
            Try
                Me.Password = rnd.GenerateRandomString(6)

                Dim reader As SqlClient.SqlDataReader = objAdminPrv.GetUserByEmail(Me)
                If reader.Read Then
                    Me.UserID = reader("UserID").ToString
                    Me.UserName = reader("UserName").ToString
                    Me.Email = reader("Email").ToString
                End If
                reader.Close()

                strReturn = Me.UserName & "|" & Me.Email & "|" & Me.Password

                If Not Me.ResetPassword() Then
                    strReturn = ""
                End If
            Catch ex As Exception

            Finally
                rnd = Nothing
            End Try
            Return strReturn
        End Function
    End Class
End Namespace